package com.xjrsoft.module.ureport.provider;


import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.bstek.ureport.provider.report.ReportFile;
import com.bstek.ureport.provider.report.ReportProvider;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.xjrsoft.module.ureport.entity.XjrRptFile;
import com.xjrsoft.module.ureport.service.IXjrRptFileService;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description 自定义报表存储器
 */

@Setter
@Component
public class MySQLProvider implements ReportProvider {
    @JsonIgnore
    private final Logger log = LoggerFactory.getLogger(getClass());
    private static final String NAME = "保存到本地数据库";
    private String prefix = "xjr:";
    private boolean disabled = false;

    @JsonIgnore
    @Autowired
    private IXjrRptFileService rptFileService;

    /**
     * 根据报表名加载报表文件
     *
     * @param file 报表名称
     * @return 返回的InputStream
     */
    @Override
    public InputStream loadReport(String file) {
        XjrRptFile ureportFileEntity = rptFileService.getByName(getCorrectName(file));
        byte[] content = ureportFileEntity.getContent();
        ByteArrayInputStream inputStream = new ByteArrayInputStream(content);
        return inputStream;
    }

    /**
     * 根据报表名，删除指定的报表文件
     *
     * @param file 报表名称
     */
    @Override
    public void deleteReport(String file) {
        rptFileService.deleteByName(getCorrectName(file));
    }

    /**
     * 获取所有的报表文件
     *
     * @return 返回报表文件列表
     */
    @Override
    @JsonIgnore
    public List<ReportFile> getReportFiles() {
        List<XjrRptFile> list = rptFileService.list();
        List<ReportFile> reportList = new ArrayList<>();
        for (XjrRptFile ureportFileEntity : list) {
            reportList.add(new ReportFile(ureportFileEntity.getName(), Date.from(ureportFileEntity.getUpdateTime().atZone(ZoneId.systemDefault()).toInstant())));
        }
        return reportList;
    }

    /**
     * 保存报表文件
     *
     * @param file    报表名称
     * @param content 报表的XML内容
     */
    @Override
    public void saveReport(String file, String content) {
        file = getCorrectName(file);
        XjrRptFile ureportFileEntity = rptFileService.getByName(file);
        LocalDateTime currentDate = LocalDateTime.now();
        byte[] contentBytes = null;
        try {
            contentBytes = content.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
        }
        if (ureportFileEntity == null) {
            ureportFileEntity = new XjrRptFile();
            ureportFileEntity.setId(IdWorker.get32UUID());
            ureportFileEntity.setName(file);
            ureportFileEntity.setContent(contentBytes);
            ureportFileEntity.setCreateTime(currentDate);
            ureportFileEntity.setUpdateTime(currentDate);
            rptFileService.save(ureportFileEntity);
        } else {
            ureportFileEntity.setContent(contentBytes);
            ureportFileEntity.setUpdateTime(currentDate);
            rptFileService.updateById(ureportFileEntity);
        }
    }

    /**
     * @return 返回存储器名称
     */
    @Override
    public String getName() {
        return NAME;
    }

    /**
     * @return 返回是否禁用
     */
    @Override
    public boolean disabled() {
        return disabled;
    }

    /**
     * @return 返回报表文件名前缀
     */
    @Override
    public String getPrefix() {
        return prefix;
    }

    /**
     * @param name
     * @return
     * @description 获取没有前缀的文件名
     */

    private String getCorrectName(String name) {
        log.info("前缀:" + prefix);
        if (name.startsWith(prefix)) {
            name = name.substring(prefix.length(), name.length());
        }
        return name;
    }


}
